package com.example.cxx.controller;

import com.example.cxx.pojo.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/* @RestController
@RequestMapping("course/api/*")
public class NoteController {

   @GetMapping("/note")



    public Result getNote() throws IOException {
        List<Note> noteList=new ArrayList<>();
        Document index=Jsoup.connect(YuMing.qianzhui+YuMing.yuming+"/tz/C0910index_1.htm").get();
        Elements noteElements=index.getElementsByClass("public_module");
        for (Element noteItem : noteElements) {
            //获取所有元素
            Elements lists=noteItem.children();
            for (Element list : lists) {
                //利用a标签判断一下是否是note公告
                Elements link=list.getElementsByTag("a");
                if(link==null){
                    continue;
                }
                Note note=new Note();
                Elements titleElements=list.getElementsByClass("line_num");
                for (Element element : titleElements) {
                    String title=element.text();
                    note.setTitle(title);
                }
                Elements timeElements=list.getElementsByTag("span");
                for (Element timeElement : timeElements) {
                    String time=timeElement.text();
                    note.setNoteTime(time);
                }
                for (Element linkItem : link) {
                    String noteLink=linkItem.attr("href");
                    note.setNoteLink(noteLink);
                }
                if(note.getTitle()!=null){
                    noteList.add(note);
                }

            }
        }
        return Result.success(noteList);
    }
    @GetMapping("/noteDetail")
    public Result getNoteDetail(String noteLink) throws IOException {
        Document index=Jsoup.connect(YuMing.qianzhui+noteLink).get();
        Element element=index.getElementById("BodyLabel");
        Elements details=element.children();
        NoteDetail noteDetail=new NoteDetail();
        List<NoteItem> detailItem=new ArrayList<>();
        for (Element detail : details) {
            if(detail.text()==""){
                continue;
            }
            String detailText=detail.text().replaceAll(Jsoup.parse("&nbsp;").text(), "");
            if(detailText!=""){
                NoteItem noteItem=new NoteItem(detailText);
                detailItem.add(noteItem);
            }
        }
        noteDetail.setListContainer(detailItem);
        return Result.success(noteDetail);
    }
}
*/